home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Games / WHDLoad / Src / programs / WCmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-24  |  3.5 KB  |  131 lines

  1. /*
  2.     $Id: WCmp.c 1.2 2000/09/24 15:17:13 jah Exp jah $
  3.  
  4.     a little, simple, quick cmp program
  5.     advantages against the thousend other cmp's out there:
  6.         - command line only
  7.         - wide output hex And ascii (this was the reason for writing)
  8.         - does not need to load whole file at once
  9.         - std c, should be eatable by every compiler
  10.  
  11.     released under GNU Public License
  12.     wepl, sometime ago ...
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #define strcasecmp stricmp
  20.  
  21. #define buflen 32768/2
  22. #define error1 { perror(argv[1]); exit(20); }
  23. #define error2 { perror(argv[2]); exit(20); }
  24. #define maxpl 16    /* displayed diffs per line */
  25.  
  26. int opt_quick=1;
  27.  
  28. /**********************/
  29.  
  30. void cmpout(unsigned char *c1, unsigned char *c2, int p1, int p2, int len) {
  31.   int j;
  32.   unsigned char *t;
  33.  
  34.   if (opt_quick == 0) return;
  35.   
  36.   printf("%06x ",p1);                /* file1 offset */
  37.   for (t=c1,j=0;j<len;j++) printf("%02x",*t++);
  38.   for (j=0;j<=2*(maxpl-len);j++) putchar(' ');
  39.   for (j=0;j<len;j++,c1++) putchar(*c1 < ' ' || *c1 > 127 ? '.' : *c1);
  40.   for (j=0;j<=maxpl-len;j++) putchar(' ');
  41.   if  (p1 != p2) printf("%06x ",p2);        /* file2 offset */
  42.   for (t=c2,j=0;j<len;j++) printf("%02x",*t++);
  43.   for (j=0;j<=2*(maxpl-len);j++) putchar(' ');
  44.   for (j=0;j<len;j++,c2++) putchar(*c2 < ' ' || *c2 > 127 ? '.' : *c2);
  45.   putchar('\n');
  46. }
  47.  
  48. /**********************/
  49.  
  50. int cmp(char *m1, char *m2, int o1, int o2, int len) {
  51.   int i, diffs=0;
  52.   char t1[maxpl+1]="", t2[maxpl+1]="";
  53.   int dc=0;    /* count of bytes in buff */
  54.   int ds=0;    /* filepos of first stored byte in buff */
  55.   
  56.   for (i=0;i<len;i++) {
  57.     if (*m1++ != *m2++) {
  58.       /* output if the new cannot appended */
  59.       if ( dc > 0 && ds+dc != i) {
  60.         cmpout(t1,t2,o1+ds,o2+ds,dc);
  61.         dc = 0;
  62.       }
  63.       /* append */
  64.       if (dc == 0) ds = i;
  65.       t1[dc]   = *(m1-1);
  66.       t2[dc++] = *(m2-1);
  67.       /* output if buff full */
  68.       if (dc == maxpl) {
  69.         cmpout(t1,t2,o1+ds,o2+ds,dc);
  70.         dc=0;
  71.       }
  72.       diffs++;
  73.     }
  74.   }
  75.   /* output if bytes left in buff */
  76.   if (dc > 0 )
  77.     cmpout(t1,t2,o1+ds,o2+ds,dc);
  78.   return diffs;
  79. }
  80.  
  81. /**********************/
  82.  
  83. long getfilesize(FILE *fp) {
  84.   fseek(fp,0,SEEK_END);
  85.   return ftell(fp);
  86. }
  87.  
  88. /**********************/
  89.  
  90. int main(int argc, char *argv[]) {
  91.   FILE *fp1,*fp2;
  92.   int len1, len2, strt1=0, strt2=0, pos1, pos2;
  93.   static char b1[buflen], b2[buflen]; /* Amiga !!! */
  94.   int l, diffs=0,i;
  95.   
  96.   if (argc < 3 || argc >4 || (argc == 4 && (opt_quick=strcasecmp(argv[3],"quick")))) {
  97.     fprintf(stderr,"Cmp 0.2 (%s)\n",__DATE__);
  98.     fprintf(stderr,"usage: %s file file [QUICK]\n",argv[0]);
  99.     exit(20);
  100.   }
  101.   
  102.   if (NULL == (fp1 = fopen(argv[1],"r"))) error1;
  103.   if (NULL == (fp2 = fopen(argv[2],"r"))) error2;
  104.   len1 = getfilesize(fp1);
  105.   len2 = getfilesize(fp2);
  106.   if (fseek(fp1,strt1,SEEK_SET)) error1;
  107.   if (fseek(fp2,strt2,SEEK_SET)) error2;
  108.   pos1 = strt1; pos2 = strt2;
  109.   
  110.   printf("       %s ",argv[1]);
  111.   for (i=strlen(argv[1]);i<49;i++) putchar(' ');
  112.   printf("%s\n",argv[2]);
  113.  
  114.   l = buflen;
  115.   while (pos1!=len1 && pos2!=len2) {
  116.     if (len1-pos1 < l) l = len1-pos1;
  117.     if (len2-pos2 < l) l = len2-pos2;
  118.     if (1 != fread(b1, l, 1, fp1)) error1;
  119.     if (1 != fread(b2, l, 1, fp2)) error2;
  120.     diffs += cmp (b1, b2, pos1, pos2, l);
  121.     pos1 += l; pos2 += l;
  122.   }
  123.   
  124.   printf(diffs == 0 ? "files are equal\n" : "files have %d differences\n",diffs);
  125.   if (len1 != len2) printf("file '%s' is %d bytes %s than file '%s'\n",
  126.     argv[1],abs(len1-len2),len1>len2?"larger":"shorter",argv[2]);
  127.   
  128.   if (diffs == 0) return 0; else return 5;
  129. }
  130.  
  131.